package com.quanyan.stadium.utils;

import com.quanyan.stadium.enums.GameSystemUnsetDoubleConfigEnum;

import java.util.*;

/**
 * Created by xiaoxiao on 16/5/27.
 *
 * 赛制算法类
 */
public class GameUtils {

    /**
     * 单败淘汰赛:根据队伍个数获取轮次
     *
     * */
    public static int washedRoundleNum(Double teamNum){
        if (teamNum.intValue() == 0)
            return 0;
        return (int)Math.ceil(Math.log(teamNum) / Math.log(2.0));
    }


    /**
     * 单败淘汰赛:根据队伍个数获取场次
     *
     * */
    public static int washedScheduleNum(Double teamNum){
        if (teamNum.intValue() == 0)
            return 0;
        return (int)(teamNum-1);
    }

    /**
     * 瑞士制:根据队伍个数获取轮次
     *  */
    public static int swissRoundleNum(Double teamNum){
        if (teamNum.intValue() == 0)
            return 0;
        return (int)Math.ceil(Math.log(teamNum) / Math.log(2.0));
    }

    /**
     * 循环赛:根据队伍个数获取轮次
     * */
    public static int circleRoundNum(Double teamNum){
        if (teamNum.intValue() == 0)
            return 0;
        teamNum = Math.ceil(teamNum);
        if (teamNum%2 == 0) {
            return (int)(teamNum - 1);
        }else {
            return teamNum.intValue();
        }
    }

    /**
     * 单循环赛:根据队伍个数获取场次
     *
     * */
    public static int circleScheduleNum(Double teamNum){
        if (teamNum.intValue() == 0)
            return 0;
        return (int)(teamNum *(teamNum-1)/2);
    }

    /**
     * 网球不固定打法,默认一个轮次
     *
     * */
    public static int unsetRoundNum(Double teamNum){
        if (teamNum.intValue() == 0)
            return 0;
        return GameSystemUnsetDoubleConfigEnum.getRoundNumByTeamNum(teamNum.intValue());
    }

    /**
     * 羽毛球:根据队伍个数获取轮次
     *
     * */
    public static int eightRoundleNum(){
        return 7;
    }


    /**
     * 获取赛事code  年份+月份+赛事类型+赛制类型+栏目id+场馆id+随机数4位
     *
     * */
    public static String generateGameCode(Byte gameEventType, Integer categoryId, Integer placeId, Byte gameSystemType){

        Calendar cal = Calendar.getInstance();
        String year = cal.get(Calendar.YEAR)+"";
        String month = String.format("%02d", (cal.get(Calendar.MONTH)) + 1);
        return year.substring(2)+month+String.format("%02d", gameEventType)+String.format("%02d", gameSystemType)+
                String.format("%02d", categoryId) + String.format("%04d", placeId) + (new Random().nextInt(9999 - 1000 + 1)+1000);
    }

    /*
    public static void main(String[] args){

        for(int i=0;i<20;i++){
            System.out.println("当前人数:"+i+"队伍数目:"+GameUtils.swissRoundleNum((double) i));
        }

        System.out.println(GameUtils.swissRoundleNum((double) 3));
        System.out.println(GameUtils.generateGameCode((byte)1, 1, 23232, (byte)0));
        System.out.println(GameUtils.generateGameCode((byte)1, 1, 23232, (byte)0));
    }*/


}
